Skip to main content

Random

Randint

  • Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
import random

# random number between [0 and 9]
res = random.randint(0,9)
print(res)

Choice

  • Return a random element from the non-empty sequence seq
import random

# random choice
res = random.choice(['apple', 'pear', 'banana'])
print(res)

Shuffle

  • Shuffle the sequence x in place.
import random

# random choice
res = ['apple', 'pear', 'banana']
random.shuffle(res)
print(res)

Sample

  • Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
import random

# give me random n choices
res = random.sample(range(100), 10)
print(res)